import { sceneMgr } from "../../../scene/SceneMgr";
import { glConfig } from "../../Config/GLConfig";
import { session } from "../../Hall/Session";
import { glUtils } from "../../lib/GLUtils";
import { UIMgr } from "../../lib/UIMgr";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Hall_Setting extends cc.Component {

    @property(cc.Node)
    BtnExitAccountNode:cc.Node = null;
    @property(cc.Sprite)
    sp_musicBtn: cc.Sprite = null;
    @property(cc.Sprite)
    sp_soundBtn: cc.Sprite = null;
    @property(cc.Sprite)
    sp_danMubtn: cc.Sprite = null;
    @property({
        type: cc.SpriteFrame,
        tooltip: "0开 1关",
    })
    sf_btn: cc.SpriteFrame[] = [];
    @property(cc.Label)
    lbl_version: cc.Label = null;

    _dmOpen = true;

    onLoad() {
        this.lbl_version.string = "版本号：" + cc.vv.GLConfig.getLocalVersion();

        let mv = this.getMusicVolume();
        this.setMusicVolume(mv)

        let sv = this.getSoundVolume();
        this.setSoundVolume(sv)

        this.refreshDMUI();
    }

    onEnable(){
        this.initDanMuData();
        if(cc.vv.sceneName == "Game"){
            this.BtnExitAccountNode.active = false;
        }else {
            this.BtnExitAccountNode.active = true;
        }
    }

    initDanMuData(){
        let danMuOpen = cc.sys.localStorage.getItem("DanMuOpen");
        if(!danMuOpen){
            cc.sys.localStorage.setItem("DanMuOpen", "1" );
            danMuOpen = "1";
        }
        if(danMuOpen == "1"){
            this._dmOpen = true;
        }else {
            this._dmOpen = false;
        }
        //发消息开关弹幕
        this.refreshDMUI();
    }

    refreshDMUI(){
        if(this._dmOpen){
            this.sp_danMubtn.spriteFrame = this.sf_btn[4];
        }else {
            this.sp_danMubtn.spriteFrame = this.sf_btn[5];
        }
    }

    onDestroy() {
        UIMgr.close(glConfig.Hall_Setting_Path)
    }
    //改变按钮
    _changeMusicBtn(sp: cc.Sprite, flag: boolean) {
        sp.spriteFrame = this.sf_btn[flag ? 0 : 1]
    }
    //改变按钮
    _changeSoundBtn(sp: cc.Sprite, flag: boolean) {
        sp.spriteFrame = this.sf_btn[flag ? 2 : 3]
    }
    //音乐
    getMusicVolume() {
        let musicVolume = Number(cc.vv.GLSettingDataModel.getMusicVolume()) ;
        return musicVolume < 0 ? 0 : (musicVolume > 1 ? 1 : musicVolume)
    }
    setMusicVolume(musicVolume) {
        this._changeMusicBtn(this.sp_musicBtn, musicVolume)
        cc.vv.GLSettingDataModel.setMusciVolume(musicVolume);
    }

    onClickDanMu(){
        this._dmOpen = !this._dmOpen;

        cc.sys.localStorage.setItem("DanMuOpen", this._dmOpen ? "1":"2" );
        //发消息开关弹幕
        this.refreshDMUI();
        cc.systemEvent.emit("DanMuControl",this._dmOpen);
    }

    //音效
    getSoundVolume() {
        let soundVolume = Number(cc.vv.GLSettingDataModel.getSoundVolume());
        return soundVolume < 0 ? 0 : (soundVolume > 1 ? 1 : soundVolume)
    }
    setSoundVolume(soundVolume) {
        this._changeSoundBtn(this.sp_soundBtn, soundVolume)
        cc.vv.GLSettingDataModel.setSoundVolume(soundVolume);
    }


    on_Music_click() {
        let v = this.getMusicVolume();
        v = v ? 0 : 1;
        this.setMusicVolume(v)
    }

    on_Sound_click() {
        let v = this.getSoundVolume();
        v = v ? 0 : 1;
        this.setSoundVolume(v)

    }

    on_Exit_click() {
        UIMgr.close(glConfig.Hall_Setting_Path)
        //不能直接关闭退出游戏
        //glUtils.exitGame();
        //改为退出游戏
        let functionOK = ()=>{
            glUtils.changeAccount();
        }
        cc.vv.UIMgr.open(cc.vv.GLConfig.alert1_path, null, null, "是否确定退出游戏", functionOK, null,);

    }

    on_Change_click() {
        UIMgr.close(glConfig.Hall_Setting_Path)
        glUtils.changeAccount();
    }

    on_Close_click() {
        UIMgr.close(glConfig.Hall_Setting_Path)
    }
}
